﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace HPDI.DrawingStandard
{
    public static class Ploter
    {


        //1. Ploter ->  DBObjectCollection
        //2. DBObjectCollection -> ObjectIdCollection

        /// <summary>
        /// 向空间写入对象
        /// </summary>
        /// <param name="objs">对象集合</param>
        /// <param name="LayoutName">布局名称，默认为空时写入模型空间</param>
        /// <returns></returns>
        public static ObjectIdCollection WriteDatabase(DBObjectCollection objs,string LayoutName="")
        {
            ObjectIdCollection ret=new ObjectIdCollection();
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction()) 
            {                   

                BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                DBDictionary lays = tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
                ObjectId LayoutID;
                if (LayoutName!="")
                {
                    LayoutID = (ObjectId)lays[LayoutName];
                    var LayoutID2 = db.GetLayoutId(LayoutName);
                    Layout theLayout=tr.GetObject(LayoutID2, OpenMode.ForWrite) as Layout;
                    btr = tr.GetObject(theLayout.BlockTableRecordId, OpenMode.ForWrite) as BlockTableRecord;
                }                
                ObjectId id;
                foreach (var item in objs)
                {
                    try
                    {
                        var ent = (Entity)item;
                        id = btr.AppendEntity(ent);
                        tr.AddNewlyCreatedDBObject(ent, true);
                        ret.Add(id);
                    }
                    catch (Exception ex)
                    {
                        //continue;
                        //throw new Exception(((Entity)item).ToString());                        
                    }
                }
                tr.Commit();
            }
            return ret;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static Extents2d GetExtends(ObjectIdCollection ids) 
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Extents2d ext;

            if (ids.Count<1)
            {
                throw new Exception();
            }
            using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
            {
                Entity ent = tr.GetObject(ids[0], OpenMode.ForRead) as Entity;
                ext = ent.GeometricExtents.C2D();
                for (int i = 0; i < ids.Count-1; i++)
                {
                    ent = tr.GetObject(ids[i+1], OpenMode.ForRead) as Entity;
                    try
                    {
                        ext = ext.Add(ent.GeometricExtents.C2D());
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                }                
            }
            return ext;            
        }






        /// <summary>
        /// 对所有Collection中的对象实施变形Mat
        /// </summary>
        /// <param name="objs"></param>
        /// <param name="mat"></param>
        public static void ExertTransform(DBObjectCollection objs, Matrix3d mat)
        {
            foreach (var item in objs)
            {
                var ent = (Entity)item;
                ent.TransformBy(mat);
            }
        }




        //public static ObjectIdCollection AddCollectionToModelSpace(DBObjectCollection ents,out Extents2d ext)
        //{
        //    ext = new Extents2d();

        //    Database db = HostApplicationServices.WorkingDatabase;

        //    ObjectIdCollection entIds = new ObjectIdCollection();//用于返回添加到模型空间中的实体ObiectId

        //    foreach (var item in ents)
        //    {
        //        db.add

        //    }
            
            
        //    //定义一个指向当前数据库的事务处理
        //    using (Transaction trans = db.TransactionManager.StartTransaction())
        //    {
        //        BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);//以读方式打开块表
        //                                                                                       //以写的方式打开模型空间块表记录
        //        BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
        //        foreach (var item in ents)
        //        {
        //            Entity ent = item as Entity;
        //            //指定要添加的实体图层
        //            ObjectId entId = btr.AppendEntity(ent);//以图形对象的信息添加到块表记录中
        //            entIds.Add(entId);
        //            trans.AddNewlyCreatedDBObject(ent, true);//把对象添加到事务处理中
        //        }
        //        trans.Commit();//提交事务处理
        //    }
        //    return entIds;//返回实体的objectid
        //}



    }
}
